

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>mindspore.ops.NLLLoss &mdash; MindSpore master documentation</title>
  

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />

  
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/doctools.js"></script>
        <script src="../../_static/language_data.js"></script>
        <script async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="mindspore.ops.OneHot" href="mindspore.ops.OneHot.html" />
    <link rel="prev" title="mindspore.ops.Mish" href="mindspore.ops.Mish.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> MindSpore
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">MindSpore Python API</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.html">mindspore</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.common.initializer.html">mindspore.common.initializer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.communication.html">mindspore.communication</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.compression.html">mindspore.compression</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.context.html">mindspore.context</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.html">mindspore.dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.audio.html">mindspore.dataset.audio</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.config.html">mindspore.dataset.config</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.text.html">mindspore.dataset.text</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.transforms.html">mindspore.dataset.transforms</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.dataset.vision.html">mindspore.dataset.vision</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.mindrecord.html">mindspore.mindrecord</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.nn.html">mindspore.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.nn.probability.html">mindspore.nn.probability</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.nn.transformer.html">mindspore.nn.transformer</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.numpy.html">mindspore.numpy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.ops.html">mindspore.ops</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.parallel.html">mindspore.parallel</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.parallel.nn.html">mindspore.parallel.nn</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.profiler.html">mindspore.profiler</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.scipy.html">mindspore.scipy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.train.html">mindspore.train</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mindspore.boost.html">mindspore.boost</a></li>
</ul>
<p class="caption"><span class="caption-text">MindSpore C++ API</span></p>
<ul>
<li class="toctree-l1"><a class="reference external" href="https://www.mindspore.cn/lite/api/zh-CN/master/api_cpp/mindspore.html">MindSpore Lite↗</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">MindSpore</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../mindspore.ops.html">mindspore.ops</a> &raquo;</li>
        
      <li>mindspore.ops.NLLLoss</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
          
            <a href="../../_sources/api_python/ops/mindspore.ops.NLLLoss.rst.txt" rel="nofollow"> View page source</a>
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="mindspore-ops-nllloss">
<h1>mindspore.ops.NLLLoss<a class="headerlink" href="#mindspore-ops-nllloss" title="Permalink to this headline">¶</a></h1>
<dl class="class">
<dt id="mindspore.ops.NLLLoss">
<em class="property">class </em><code class="sig-prename descclassname">mindspore.ops.</code><code class="sig-name descname">NLLLoss</code><span class="sig-paren">(</span><em class="sig-param">reduction=&quot;mean&quot;</em><span class="sig-paren">)</span><a class="headerlink" href="#mindspore.ops.NLLLoss" title="Permalink to this definition">¶</a></dt>
<dd><p>获取预测值和目标值之间的负对数似然损失。</p>
<p>reduction=none时，负对数似然损失如下：</p>
<div class="math notranslate nohighlight">
\[\ell(x, t)=L=\left\{l_{1}, \ldots, l_{N}\right\}^{\top},
\quad l_{n}=-w_{t_{n}} x_{n, t_{n}},
\quad w_{c}=\text { weight }[c] \cdot 1\]</div>
<p>其中， <span class="math notranslate nohighlight">\(x\)</span> 表示预测值， <span class="math notranslate nohighlight">\(t\)</span> 表示目标值， <span class="math notranslate nohighlight">\(w\)</span> 表示权重，N表示batch size， <span class="math notranslate nohighlight">\(c\)</span> 限定范围为[0, C-1]，表示类索引，其中 <span class="math notranslate nohighlight">\(C\)</span> 表示类的数量。</p>
<p>reduction不为’none’（默认为’mean’），则</p>
<div class="math notranslate nohighlight">
\[\begin{split}\ell(x, t)=\left\{\begin{array}{ll}
\sum_{n=1}^{N} \frac{1}{\sum_{n=1}^{N} w_{t n}} l_{n}, &amp; \text { if reduction }=\text { 'mean'; } \\
\sum_{n=1}^{N} l_{n}, &amp; \text { if reduction }=\text { 'sum' }
\end{array}\right.\end{split}\]</div>
<p><strong>参数：</strong></p>
<ul class="simple">
<li><p><strong>reduction</strong> (str) - 指定应用于输出结果的计算方式，比如’none’、’mean’，’sum’，默认值：”mean”。</p></li>
</ul>
<p><strong>输入：</strong></p>
<ul class="simple">
<li><p><strong>logits</strong> (Tensor) - 输入预测值，shape为 <span class="math notranslate nohighlight">\((N, C)\)</span> 。数据类型仅支持float32或float16。</p></li>
<li><p><strong>labels</strong> (Tensor) - 输入目标值，shape为 <span class="math notranslate nohighlight">\((N,)\)</span> 。数据类型仅支持int32。</p></li>
<li><p><strong>weight</strong> (Tensor) - 指定各类别的权重，shape为 <span class="math notranslate nohighlight">\((C,)\)</span> ，数据类型仅支持float32或float16。</p></li>
</ul>
<p><strong>输出：</strong></p>
<p>由 <cite>loss</cite> 和 <cite>total_weight</cite> 组成的2个Tensor的元组。</p>
<ul class="simple">
<li><p><strong>loss</strong> (Tensor) - 当 <cite>reduction</cite> 为’none’且 <cite>logits</cite> 为2维Tensor时， <cite>loss</cite> 的shape为 <span class="math notranslate nohighlight">\((N,)\)</span> 。否则， <cite>loss</cite> 为scalar。数据类型与 <cite>input’s</cite> 相同。</p></li>
<li><p><strong>total_weight</strong> (Tensor) - <cite>total_weight</cite> 是scalar，数据类型与 <cite>weight’s</cite> 相同。</p></li>
</ul>
<p><strong>异常：</strong></p>
<ul class="simple">
<li><p>** TypeError** - <cite>logits</cite> 或 <cite>weight</cite> 的数据类型既不是float16也不是float32， <cite>labels</cite> 不是int32。</p></li>
<li><p>** ValueError** - <cite>logits</cite> 不是一维或二维Tensor， <cite>labels</cite> 和 <cite>weight</cite> 不是一维Tensor。 <cite>logits</cite> 是二维Tensor时， <cite>logits</cite> 的第一个维度不等于 <cite>labels</cite> ， <cite>logits</cite> 的第二个维度不等于 <cite>weight</cite> 。 <cite>logits</cite> 是一维Tensor时， <cite>logits</cite> 、 <cite>labels</cite> 和 <cite>weight</cite> 的维度应该相同。</p></li>
</ul>
<p><strong>支持平台：</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">Ascend</span></code> <code class="docutils literal notranslate"><span class="pre">GPU</span></code></p>
<p><strong>样例：</strong></p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">logits</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([[</span><span class="mf">0.5488135</span><span class="p">,</span> <span class="mf">0.71518934</span><span class="p">],</span>
<span class="gp">... </span>                          <span class="p">[</span><span class="mf">0.60276335</span><span class="p">,</span> <span class="mf">0.5448832</span><span class="p">],</span>
<span class="gp">... </span>                          <span class="p">[</span><span class="mf">0.4236548</span><span class="p">,</span> <span class="mf">0.6458941</span><span class="p">]])</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">labels</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">])</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">weight</span> <span class="o">=</span> <span class="n">Tensor</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">0.3834415</span><span class="p">,</span> <span class="mf">0.79172504</span><span class="p">])</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">nll_loss</span> <span class="o">=</span> <span class="n">ops</span><span class="o">.</span><span class="n">NLLLoss</span><span class="p">(</span><span class="n">reduction</span><span class="o">=</span><span class="s2">&quot;mean&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">loss</span><span class="p">,</span> <span class="n">weight</span> <span class="o">=</span> <span class="n">nll_loss</span><span class="p">(</span><span class="n">logits</span><span class="p">,</span> <span class="n">labels</span><span class="p">,</span> <span class="n">weight</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">loss</span><span class="p">)</span>
<span class="go">-0.52507716</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">weight</span><span class="p">)</span>
<span class="go">1.1503246</span>
</pre></div>
</div>
</dd></dl>

</div>


           </div>
           
          </div>
          <footer>
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
        <a href="mindspore.ops.OneHot.html" class="btn btn-neutral float-right" title="mindspore.ops.OneHot" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
        <a href="mindspore.ops.Mish.html" class="btn btn-neutral float-left" title="mindspore.ops.Mish" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2021, MindSpore.

    </p>
  </div>
    
    
    
    Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>